package com.yuyou.activity.group.service.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.yuyou.activity.group.service.entity.ActivityGroupbuyOrder;
import com.yuyou.activity.group.service.service.IActivityGroupbuyOrderService;
import com.yuyou.activity.util.enums.ActivityGroupJobEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.yuyou.activity.group.service.entity.ActivityGroupJob;
import com.yuyou.activity.group.service.entity.ActivityGroupUser;
import com.yuyou.activity.group.service.mapper.ActivityGroupJobMapper;
import com.yuyou.activity.group.service.service.IActivityGroupJobService;
import com.yuyou.activity.group.service.service.IActivityGroupUserService;

@Service
public class ActivityGroupJobServiceImpl implements IActivityGroupJobService {

    @Autowired
    private ActivityGroupJobMapper mapper;

    @Autowired
    private IActivityGroupUserService activityGroupUserService;

    @Autowired
    private IActivityGroupbuyOrderService activityGroupbuyOrderService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ActivityGroupJob record) {
        record.setNeedRetry(ActivityGroupJobEnums.needRetry.getByType(record.getType()));
        record.setMaxTime(ActivityGroupJobEnums.maxTime.getByType(record.getType()));
        record.setHasTime(0);
        record.setInitTime(record.getCreateTime());
        return mapper.insert(record);
    }

    @Override
    public ActivityGroupJob selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivityGroupJob> findLoadedJob(Long allotTempId) {
        return mapper.findLoadedJob(allotTempId);
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityGroupJob record) {
        if(ActivityGroupJobEnums.status.fail.getValue() == record.getStatus()){
            ActivityGroupJob commJob = mapper.selectByPrimaryKey(record.getId());
            if(commJob != null){
                if(commJob.getNeedRetry() == 1 && commJob.getHasTime() != commJob.getMaxTime()){//需要重试
                    record.setStatus(ActivityGroupJobEnums.status.init.getValue());
                    record.setHasTime(commJob.getHasTime()+1);

                    //计算下次运行时间（增加间隔时间）
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MINUTE,ActivityGroupJobEnums.interval.getMaxByTimes(commJob.getHasTime()));
                    record.setCreateTime(calendar.getTime());
                }
            }
        }
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateLoadedJob(Long allotTempId) {
        return mapper.updateLoadedJob(allotTempId);
    }

    @Override
    public int updateJobStatus(Long fId, Integer type, String tag, Integer status,String remark) {
        if(ActivityGroupJobEnums.status.fail.getValue() == status){
            ActivityGroupJob commJob = mapper.selectByFId(fId , type , tag);
            if(commJob != null){
                if(commJob.getNeedRetry() == 1 && commJob.getHasTime() != commJob.getMaxTime()){//需要重试
                    commJob.setStatus(ActivityGroupJobEnums.status.init.getValue());
                    commJob.setHasTime(commJob.getHasTime()+1);
                    commJob.setTag(tag);
                    commJob.setType(type);
                    commJob.setRemark(remark);

                    //计算下次运行时间（增加间隔时间）
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MINUTE,ActivityGroupJobEnums.interval.getMaxByTimes(commJob.getHasTime()));
                    commJob.setCreateTime(calendar.getTime());

                    return mapper.updateByPrimaryKeySelective(commJob);
                }
            }
        }
        return mapper.updateJobStatus(fId , type ,tag ,status,remark);
    }

    @Override
    @Transactional
    public void insertJob(List<ActivityGroupUser> list, List<ActivityGroupJob> jobs) {
        if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(jobs)){
            jobs.forEach(record -> {
                record.setNeedRetry(ActivityGroupJobEnums.needRetry.getByType(record.getType()));
                record.setMaxTime(ActivityGroupJobEnums.maxTime.getByType(record.getType()));
                record.setHasTime(0);
                record.setInitTime(record.getCreateTime());
            });
            mapper.insertBatch(jobs);
            activityGroupUserService.updateStatusByList(list);
        }
    }

    @Override
    @Transactional
    public void insertJobShipin(List<ActivityGroupbuyOrder> list, List<ActivityGroupJob> jobs , Integer tag) {
        if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(jobs)){
            jobs.forEach(record -> {
                record.setHasTime(0);
                record.setInitTime(record.getCreateTime());
                record.setNeedRetry(ActivityGroupJobEnums.needRetry.getByType(record.getType()));
                record.setMaxTime(ActivityGroupJobEnums.maxTime.getByType(record.getType()));
            });
            mapper.insertBatch(jobs);
            activityGroupbuyOrderService.updateStatusByList(list , tag);
        }
    }

    @Override
    public void updateJob(Integer status, Long id, String remark) {
        if(id != null){
            ActivityGroupJob job = new ActivityGroupJob();
            job.setId(id);
            job.setStatus(status);
            job.setRemark(remark);

            if(ActivityGroupJobEnums.status.fail.getValue() == status){
                ActivityGroupJob commJob = mapper.selectByPrimaryKey(id);
                if(commJob != null){
                    if(commJob.getNeedRetry() == 1 && commJob.getHasTime() != commJob.getMaxTime()){//需要重试
                        commJob.setStatus(ActivityGroupJobEnums.status.init.getValue());
                        commJob.setHasTime(commJob.getHasTime()+1);
                        commJob.setRemark(remark);

                        //计算下次运行时间（增加间隔时间）
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(new Date());
                        calendar.add(Calendar.MINUTE,ActivityGroupJobEnums.interval.getMaxByTimes(commJob.getHasTime()));
                        commJob.setCreateTime(calendar.getTime());

                        mapper.updateByPrimaryKeySelective(commJob);
                        return;
                    }
                }
            }

            mapper.updateByPrimaryKeySelective(job);
        }
    }


}